""" 制作缩略图"""
import cv2 as cv
import numpy as np


class thumbnail_img():
    # 方式二
    def __init__(self, filename):
        self.filename = filename

    def get_img(self):
        face_cascade = cv.CascadeClassifier('./haarcascade/haarcascade_frontalface_alt.xml')  # 初始化检测器
        img = cv.imdecode(np.fromfile(self.filename, dtype=np.uint8), -1)  ####读取图片

        if len(np.shape(img)) == 3:
            self.gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
            h, w,_ = np.shape(img)
            # print("i am here 1")
        if len(np.shape(img)) == 2:
            self.gray = img
            h, w = np.shape(img)
        faces_num = face_cascade.detectMultiScale(self.gray)
        # print("11111111111")
        if len(faces_num) == 1:  ####照片里有一个人可以作为训练数据
            # print("i am here")
            for (face_x, face_y, face_w, face_h) in faces_num:

                if len(np.shape(img)) == 3:
                    img_pro =   img[face_y:face_y + face_h, face_x:face_x + face_w,:]
                    print("i am here")
                else:
                    img_pro = img[face_y:face_y + face_h, face_x:face_x + face_w]
            # for (face_x, face_y, face_w, face_h) in faces_num:  ########人脸的所有位置
            #     new_img = cv.resize(gray[face_y:face_y + face_h, face_x:face_x + face_w], (self.h, self.w),
            #                             interpolation=cv.INTER_NEAREST)
            # print("111111")
            cv.imwrite("./temp/0.jpg",img_pro)
            # print("22222")
            return img_pro ,True

        else:
            print("人脸过多无法展示")
            return -1,False
